#include "mainwindow.h"
#include "common.h"
#include "ui_mainwindow.h"
#include "imagedialog.h"
#include "imageInfo.h"
#include "notedb.h"
#include "imagelabel.h"

#include <QApplication>
#include <QFileDialog>
#include <QGridLayout>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QSqlRecord>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QIcon trayImg = QIcon(":/images/icon.png");
    setWindowIcon(trayImg);
    trayIcon.setIcon(trayImg);
    trayIcon.setToolTip("我的笔记");

    initMenu();
    trayIcon.setContextMenu(trayIconMenu);
    trayIcon.show();
    trayIcon.showMessage("showMessage","启动成功！",QSystemTrayIcon::MessageIcon::Information,500);

//    connect(ui->button, &QPushButton::clicked,this, &MainWindow::startProcess);
//    connect(ui->button, SIGNAL(clicked()),this, SLOT(startProcess()));

    layout = new QGridLayout(this);
    ui->mainWidget->setLayout(layout);
    setLayout(layout);
    initDialog();
}

MainWindow::~MainWindow()
{
    NoteDB::closeDB();
    delete ui;
}

void MainWindow::initMenu()
{
    reloadAction = new QAction(STR_RELOAD, this);
    addNewAction = new QAction(STR_ADD_NEW, this);
    closeAction = new QAction(STR_CLOSE, this);
    quitAction = new QAction(STR_QUIT, this);
    showMainWinAction = new QAction(STR_SHOW_MAIN_WIN, this);

//    connect(quitAction, SIGNAL(triggered()), this, SLOT(quitApp()));
    connect(quitAction, &QAction::triggered, this, &MainWindow::quitApp);
    connect(addNewAction, SIGNAL(triggered()), this, SLOT(doAddNewAction()));
    connect(reloadAction, SIGNAL(triggered()), this, SLOT(doReloadAction()));
    connect(closeAction, SIGNAL(triggered()), this, SLOT(doCloseAction()));
    connect(showMainWinAction, SIGNAL(triggered()), this, SLOT(doshowMainWinAction()));

    this->trayIconMenu = new QMenu();
    trayIconMenu->addAction(addNewAction);
    trayIconMenu->addAction(reloadAction);
    trayIconMenu->addAction(showMainWinAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction);



    auto menu = ui->menuMain;
    menu->addAction(addNewAction);
    menu->addAction(reloadAction);
    menu->addSeparator();
    menu->addAction(quitAction);

//    QAction a("");
//    menu->addAction(&a);
}

void MainWindow::initDialog()
{
    qDebug() << "initDialog";
    QSqlQuery query = NoteDB::findAll();

    while(query.next()){
        QSqlRecord record = query.record();
        auto id = record.value("id").toString();
        auto imgFileName = record.value("imgFileName").toString();
        auto width = record.value("width").toInt();
        auto height = record.value("height").toInt();
        auto left = record.value("left").toInt();
        auto top = record.value("top").toInt();
        auto dlg = new ImageDialog(id, imgFileName, this);
        dlg->resize(width, height);
        dlg->move(left, top);
        dlg->show();

        createLabel(imgFileName, id);
        CommonData::dlgMap[id] = dlg;

//        imgCount++;
    }
}
/**
 * @brief MainWindow::createLabel
 * @param imgFile
 * @param id
 */
void MainWindow::createLabel(QString imgFile, QString id, int num)
{
    ImageLabel* label = new ImageLabel(this);
    label->setMaximumHeight(200);
    label->setMaximumWidth(300);
    label->setAutoFillBackground(true);

    QPixmap *pixmap = new QPixmap(imgFile);
    pixmap->scaled(label->size(), Qt::KeepAspectRatio);
    label->setScaledContents(true);
    label->setPixmap(*pixmap);
    label->setObjectName(id);
    label->setMargin(5);


    auto imgCount = num == -1? CommonData::dlgMap.size(): num;
    auto row = (int)(imgCount / COUNT_PER_ROW);
    auto col = imgCount % COUNT_PER_ROW;
    layout->addWidget(label, row, col);
    connect(label, SIGNAL(imageChange()), this, SLOT(onImageChange()));
    CommonData::imgMap[id] = label;

}

void MainWindow::quitApp()
{
    qDebug() << "quit";

    QApplication::quit();
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    qDebug() << "QCloseEvent";
    doCloseAction();
    event->ignore();
}

void MainWindow::doReloadAction()
{
    qDebug() << "on_reloadAction_triggered";
    foreach (auto value, CommonData::dlgMap){
        value->show();
    }
}

void MainWindow::doAddNewAction()
{
    qDebug() << "on_btnAddImg_clicked";
    auto afile = QFileDialog::getOpenFileName(this, "请选择图片", "", "图片(*.jpg *.png);;All(*.*);;");
    qDebug() << "select file: " << afile;

    if (!afile.isEmpty()){
        auto dlg = new ImageDialog(this);
        dlg->setImage(afile);
        dlg->show();

        createLabel(afile, dlg->getId());
        CommonData::dlgMap[dlg->getId()] = dlg;
    }

}

void MainWindow::doCloseAction()
{
    setVisible(false);
}

void MainWindow::doshowMainWinAction()
{
    show();
}

void MainWindow::onImageChange()
{
    foreach (auto value, CommonData::imgMap){
        value->deleteLater();
    }

    CommonData::imgMap.clear();
    qDebug() << "CommonData::imgMap.size" << CommonData::imgMap.size();

    auto n = 0;
    QMapIterator<QString, QDialog*> i(CommonData::dlgMap);

    while (i.hasNext()) {
        i.next();
        auto dlg = (ImageDialog*)(i.value());
        auto data = dlg->getImageInfo();
        createLabel(data.imgFileName, data.id, n);
        n++;
    }


}



